<template>
  <a-modal
    :title="title"
    :width="1000"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="id"
          :hidden="true"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            v-decorator="['id']"
            :readonly="editable || showable"
          />
        </a-form-item>
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="菜单名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                placeholder="请输入菜单名称"
                :readonly="showable"
                v-decorator="['title', {rules: [{required: true, message: '请输入菜单名称!'}]}]" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="菜单编号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                :readonly="showable"
                placeholder="请输入菜单编号"
                v-decorator="['componentName', {rules: [{required: true, message: '请输入菜单编号'}]}]" />
            </a-form-item>
          </a-col>

        </a-row>
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="所属应用"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <biz-select
                placeholder="请选择应用分类"
                :dicUrl="'/sys/app/dropdown'"
                v-decorator="['application', {rules: [{required: true, message: '请选择应用分类'}]}]" />
            </a-form-item>
          </a-col>

          <a-col :md="12" :sm="24">
            <a-form-item
              label="菜单类型"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <biz-radio-group
                placeholder="请选择菜单类型"
                :dicUrl="'/sys/dict/type/menuType'"
                @change="handleChangeMenu"
                v-decorator="['type', {rules: [{required: true, message: '请选择菜单类型'}]}]" />
            </a-form-item>
          </a-col>

        </a-row>
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="父级菜单"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <biz-select-tree
                placeholder="请选择父级菜单"
                :dicUrl="'/sys/permission/select'"
                v-decorator="['parentId',{rules: [{required: true, message: '请选择父级菜单'}]}]"
              >
              </biz-select-tree>
            </a-form-item>
          </a-col>
        </a-row>

        <a-divider />
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="组件地址"
              :labelCol="labelCol"
              v-if="menuType !== '2'"
              :wrapperCol="wrapperCol"
            >
              <a-input
                placeholder="请输入组件地址"
                :readonly="showable"
                v-decorator="['component', {rules: [{required: true, message: '请输入组件地址'}]}]" />
            </a-form-item>
          </a-col>

          <a-col :md="12" :sm="24">
            <a-form-item
              label="路由地址"
              :labelCol="labelCol"
              v-if="menuType !== '2'"
              :wrapperCol="wrapperCol"
            >
              <a-input
                placeholder="请输入路由地址"
                :readonly="showable"
                v-decorator="['url', {rules: [{required: true, message: '请输入路由地址'}]}]" />
            </a-form-item>
          </a-col>
        </a-row>

        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="是否隐藏"
              v-if="menuType !== '2'"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-radio-group :options="isOrNoList" v-decorator="['hidden', {rules: [{required: true, message: '请选择页面是否隐藏'}]}]"  />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="图标"
              :labelCol="labelCol"
              v-if="menuType !== '2'"
              :wrapperCol="wrapperCol"
            >
              <a-input
                :disabled="true"
                v-decorator="['icon']" >
                <a-icon slot="addonAfter" @click="openIconSele()" type="setting" />
              </a-input>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="权限代码"
              :labelCol="labelCol"
              v-if="menuType === '2'"
              :wrapperCol="wrapperCol"
            >
              <a-input
                placeholder="请输入权限代码"
                :readonly="showable"
                v-decorator="['perms', {rules: [{required: true, message: '请输入权限代码'}]}]" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">

          </a-col>
        </a-row>

        <a-row :gutter="24">
          <a-col :md="12" :sm="24">

          </a-col>
          <a-col :md="12" :sm="24">

          </a-col>
        </a-row>

      </a-form>
    </a-spin>

    <template slot="footer">
      <div v-show="!showable">
        <a-button key="cancel" @click="handleCancel">取消</a-button>
        <a-button key="forward" :loading="confirmLoading" type="primary" @click="handleOk">保存</a-button>
      </div>
    </template>

    <a-modal
      :width="850"
      :visible="visibleIcon"
      @cancel="handleCancelIcon"
      footer=""
      :mask="false"
      :closable="false"
      :destroyOnClose="true"
    >
      <icon-selector v-model="currentSelectedIcon" @change="handleIconChange"/>
    </a-modal>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { addObj, putObj, getObj } from '@api/sys/menu'
import { FormMixin } from '@/mixins/FormMixin'
import IconSelector from '@/components/IconSelector'
const isOrNoList = [
  {
    label: '是',
    value: '1'
  },
  {
    label: '否',
    value: '0'
  }
]
export default {
  name: 'PermissionAddOrUpdate',
  mixins: [FormMixin],
  components: {
    IconSelector
  },
  data () {
    return {
      isOrNoList,
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      treeData: [],
      menuType: '0',
      visibleIcon: false,
      currentSelectedIcon: 'pause-circle'
    }
  },
  methods: {

    edit (tmpRecord, type, permsType) {
      this.visible = true
      const { form: { setFieldsValue, resetFields } } = this
      if (['edit', 'show'].includes(type)) {
        this.confirmLoading = true
        getObj(tmpRecord.id).then(res => {
          const record = res.data
          record.parentId = String(record.parentId)
          this.confirmLoading = false
          this.$nextTick(() => {
            setFieldsValue(pick(record, ['id', 'title', 'type', 'perms', 'parentId', 'url', 'component', 'componentName', 'keepAlive','hidden','application','icon']))
          })
        })
      } else {
        resetFields()
      }
    },
    handleChangeMenu(e){
      this.menuType = e.target.value
      // 根据不同的菜单类型进行显示或者默认值
      // 如果是目录
      if(this.menuType === 0){
        this.form.resetFields(`component`, [])
        this.form.getFieldDecorator('component',{initialValue: 'PageView'})
      }else{
        this.form.getFieldDecorator('component',{initialValue: ''})
      }




    },
    handleOk () {
      this.form.validateFields(async (err, values) => {
        if (!err) {
          this.confirmLoading = true
          try {
            if (this.type === 'add') {
              await addObj(values)
            } else if (this.type === 'edit') {
              await putObj(values)
            }
            setTimeout(() => {
              this.confirmLoading = false
              this.$emit('ok')
              this.visible = false
            }, 1500)
          } catch (err) {
            this.$message.error(err.msg)
            this.confirmLoading = false
          }
        }
      })
    },
    // 选择图表弹出
    openIconSele () {
      this.visibleIcon = true
    },
    // 取消选择图表
    handleCancelIcon () {
      this.visibleIcon = false
    },
    // 选择图表
    handleIconChange (icon) {
      this.form.resetFields(`icon`,icon);
      this.form.getFieldDecorator('icon', { initialValue: icon })
      this.visibleIcon = false
    },
  }
}
</script>

<style scoped>

</style>
